package com.zifan.sever.controller;

import com.zifan.sever.bean.Menu;
import com.zifan.sever.bean.Role;
import com.zifan.sever.service.IMenuRoleService;
import com.zifan.sever.service.IMenuService;
import com.zifan.sever.service.IRoleService;
import com.zifan.sever.utils.RespBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限控制器
 * @author Zifan
 * @date 2021年04月18日20:26
 */
@RestController
@RequestMapping("/system/basic/permiss")
public class PermissionsController {
    @Autowired
    private IRoleService roleService;

    @Autowired
    private IMenuService menuService;

    @Autowired
    private IMenuRoleService menuRoleService;

    @ApiOperation(value = "获取所有角色")
    @GetMapping("/")
    public List<Role> getAllRoles(){
        return roleService.list();
    }

    @ApiOperation(value = "添加角色")
    @PostMapping("/role")
    public RespBean addRole(@RequestBody Role role){
        if (roleService.saveWithRoleName(role)){
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }

    @ApiOperation(value = "删除角色")
    @DeleteMapping("/role/{rid}")
    public RespBean deleteRole(@PathVariable Integer rid){
        if (roleService.removeById(rid)){
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @ApiOperation(value = "查询所有的菜单列表，每个菜单中带有属于自己的子菜单")
    @GetMapping("/menus")
    public List<Menu> getMenusWithChildren(){
        return menuService.getMenusWithChildren();
    }

    @ApiOperation(value = "查出该角色id对应的菜单列表")
    @GetMapping("/mid/{rid}")
    public List<Integer> getMenusIdByRoleId(@PathVariable Integer rid){
        return menuRoleService.getMenusIdByRoleId(rid);
    }

    @ApiOperation(value = "更新角色的菜单列表")
    @PutMapping("/")
    public RespBean updateRolesMenu(Integer rid,Integer[] mids){
        return menuRoleService.updateRolesMenu(rid,mids);
    }


}
